package my.kwantlen.assignment2;

import org.apache.commons.net.ftp.*;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import android.content.SharedPreferences;
import my.kwantlen.assignment2.*;


public class Assignment2finalActivity extends Activity {
	static FTPClient mFTPClient;
	TextView tView;	
	String host; 
	String username;
	String password;
	final int mode = Activity.MODE_PRIVATE;
	
	int port; 
    @Override  
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SharedPreferences mySharedPreferences = getSharedPreferences("MySettings_001", mode);
        if (mySharedPreferences != null) {
        host = mySharedPreferences.getString("serverIp", "193.43.36.131");
		username = mySharedPreferences.getString("userName", "anonymous");
		password = mySharedPreferences.getString("userPassword", "anonymous");
		port = mySharedPreferences.getInt("port", 21);
		TextView host_widget = 
				(TextView)  this.findViewById(R.id.host_widget); 
				host_widget.setText(host); 
		TextView username_widget = 
				(TextView)  this.findViewById(R.id.username_widget); 
		username_widget.setText(username); 
		TextView password_widget = 
				(TextView)  this.findViewById(R.id.password_widget); 
				password_widget.setText(password); 
		TextView port_widget = 
				(TextView)  this.findViewById(R.id.port_widget); 
				port_widget.setText(port + ""); 
        }
        final Button buttonconnect = (Button) findViewById(R.id.btnConnect); 
        buttonconnect.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	
                connect();
            }
        });
        }
    		     
    public void connect() {
 	  getinfo();    	    
 	  if (ftpConnect(host, username, password ,port)){
 		  
 		  
 		 Intent i = new Intent();
         i.setClassName("my.kwantlen.assignment2", "my.kwantlen.assignment2.ArrayAd");
         startActivity(i);
         }
    	}
            

	public void getinfo(){
			
			
    	   TextView host_widget = (TextView) findViewById(R.id.host_widget);
    	   host = host_widget.getText().toString();
    	   TextView username_widget = (TextView) findViewById(R.id.username_widget);
    	   username = username_widget.getText().toString();
    	   TextView password_widget = (TextView) findViewById(R.id.password_widget);
    	   password = password_widget.getText().toString();
    	   TextView port_widget = (TextView) findViewById(R.id.port_widget);
    	   port = Integer.parseInt(port_widget.getText().toString());    	   
       }
    
       public boolean ftpConnect(String host, String username, String password, int port) {
    		try {
    			mFTPClient = new FTPClient();
    			// connecting to the host
    			mFTPClient.connect(host, port);

    			// now check the reply code, if positive mean connection success
    			if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
    				// login using username & password
    				boolean status = mFTPClient.login(username, password);
    				mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
    				mFTPClient.enterLocalPassiveMode();

    				return status;
    			}
    		} catch (Exception e) {
    			// You can display a dialog showing connection failure
    		}
    		return false;
    	}

        public boolean ftpDisconnect() {
    		try {
    			mFTPClient.logout();
    			
    			mFTPClient.disconnect();
    			tView.setText("Successful Disconnect");
    			return true;
    		} catch (Exception e) {
    			// Log.d(TAG,
    			// "Error occurred while disconnecting from ftp server.");
    			}
    		return false;
    	}
        public static String[] ftpGetCurrentWorkingDirectory() {
    		
        	try {
    			String[] workingDir = mFTPClient.listNames();
    			return workingDir;
    		} catch (Exception e) {
    			// Log.d(TAG, "Error: could not get current working directory.");
    			// Display a dialog saying that cannot get directory from server
    			return null;
    		}

    	}             

    	
        
public static String[] changeWorkingDirectory(String newDir) {
    		
        	try {
        		String section = new String();
        		section = mFTPClient.printWorkingDirectory() + "/" + newDir + "/";
        		
        		mFTPClient.changeWorkingDirectory(section);
    			String[] workingDir = mFTPClient.listNames();
    			return workingDir;
    		} catch (Exception e) {
    			// Log.d(TAG, "Error: could not get current working directory.");
    			// Display a dialog saying that cannot get directory from server
    			return null;
    		}
    	}


public static String[] goUpDirectory() {
	
	try {
		
		
		
		mFTPClient.changeToParentDirectory();
		String[] workingDir = mFTPClient.listNames();
		return workingDir;
	} catch (Exception e) {
		// Log.d(TAG, "Error: could not get current working directory.");
		// Display a dialog saying that cannot get directory from server
		return null;
	}
}


 
public static boolean ftpDownload(String srcFilePath, String desFilePath)
{
    boolean status = false;
    try {
        FileOutputStream desFileStream = new FileOutputStream(desFilePath);
        status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
        desFileStream.close();

        return status;
    } catch (Exception e) {
       
    }

    return status;
}


}